﻿using blackhouse.BlackApart.Business;
using blackhouse.BlackApart.Business.Discounts;
using blackhouse.BlackApart.Business.Prices;
using blackhouse.BlackApart.Business.Reservations;
using blackhouse.BlackApart.Data.Caching;
using blackhouse.Data;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;

namespace blackhouse.Aparts.Web.Discounts {
    public class FirstTimeDiscount : IDiscount {

        #region Fields

        private Dictionary<int, List<FirstTimeData>> data;
        private NemoCache cache = new NemoCache(TimeSpan.FromMinutes(2), ConfigurationManager.AppSettings["MSSQLConnectionString"]);
        private AdoDataHelper dataHelper = new MsSqlAdoDataHelper(ConfigurationManager.AppSettings["MSSQLConnectionString"]);

        #endregion

        #region Methods

        public void Apply(Variant variant, DateTime arrivalDate, DateTime departureDate, int peoples) {
            IEnumerable<FirstTimeData> data = this.GetData(variant.GetGroups()[0].Id);
            this.ApplyFirstTimeDiscount(variant, arrivalDate, departureDate, data);
        }

        private void ApplyFirstTimeDiscount(Variant variant, DateTime arrivalDate, DateTime departureDate, IEnumerable<FirstTimeData> data) {
            if (data.Count() == 0) return;
            var q = from d in data where (arrivalDate.Date - DateTime.Now.Date).TotalDays >= d.Days orderby d.Days descending select d;
            FirstTimeData ft = q.FirstOrDefault();
            if (ft.Rabat != 0) {
                IPriceRabat firstTimeRabat = Factory.Instance().GetPriceRabat("firsttime");
                firstTimeRabat.OverrideRabatValue(ft.Rabat);
                variant.Price.ApplyPriceRabat(firstTimeRabat);
            }
        }

        public void SetXmlConfiguration(System.Xml.XmlElement configurationNode) {

        }

        private IEnumerable<FirstTimeData> GetData(int apartmentTypeId) {
            if (data == null)
                this.data = this.GetDbData();
            if (this.data.ContainsKey(apartmentTypeId))
                return this.data[apartmentTypeId];
            else
                return new FirstTimeData[0];
        }

        protected virtual Dictionary<int, List<FirstTimeData>> GetDbData() {
            cache.Reset();
            cache.SetKey("FirstTimeDiscount");
            cache.SetDependencyTables("T373");
            Dictionary<int, List<FirstTimeData>> data = cache.GetData<Dictionary<int, List<FirstTimeData>>>();
            if (data == null) {
                data = new Dictionary<int, List<FirstTimeData>>();
                this.dataHelper.Reset();
                this.dataHelper.Query = "SELECT * FROM T373 WHERE del=0";
                IDataReader reader = this.dataHelper.Execute();
                while (reader.Read()) {
                    int apartmentTypeId = Convert.ToInt32(reader["typ_apartamentu"]);
                    FirstTimeData lfData = new FirstTimeData();
                    lfData.Days = Convert.ToInt32(reader["od_ilu_first"]);
                    lfData.Rabat = Convert.ToSingle(reader["rabat"]);
                    if (!data.ContainsKey(apartmentTypeId))
                        data.Add(apartmentTypeId, new List<FirstTimeData>());
                    data[apartmentTypeId].Add(lfData);
                }
                reader.Close();
                cache.SetCaching(data);
            }
            return data;
        }

        #endregion

        #region Struct

        protected struct FirstTimeData {
            public int Days;
            public float Rabat;
        }

        #endregion

    }
}